package com.outsource.framework.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.outsource.framework.domain.custom.Ad;
import com.outsource.framework.model.services.TadService;
import com.outsource.framework.util.common.Response;

@Controller
public class AdContrller {

	@Autowired
	TadService service;
	
	@Autowired
	MessageSource messageSource;
	
	@RequestMapping(value = "/WoWebAD",method = {RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Map<String, Object> WoWebADAll(
			HttpServletRequest request){
		
		String responseCode = getMessages("lable.code.success");
		String responseMessages = getMessages("lable.success");
		
		List<Object[]> temp = service.getByAdAll();
		List<Ad> responseEntry = new ArrayList<Ad>();
		for (Iterator<Object[]> iterator = temp.iterator(); iterator.hasNext();) {
			Object[] objects = (Object[]) iterator.next();
			String adname = String.valueOf(objects[0]);
			String adlink = String.valueOf(objects[1]);
			String adcontent = String.valueOf(objects[2]);
			String adpic = String.valueOf(objects[3]);
			Ad ad = new Ad(adpic,adlink);
			responseEntry.add(ad);
		}
		Response response = new Response();
		Map<String, Object> responsemap = response.getResponse(responseCode, responseMessages, responseEntry);
		return responsemap;
	}
	
	/**
	 * get zh.properties content
	 * @param code
	 * @return
	 */
	public String getMessages(String code){
		return messageSource.getMessage(code, null,Locale.SIMPLIFIED_CHINESE);
	}
}
